package customview.ike.com.customviewproject.widget.customLayoutManager;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 *@author:ike
 *@function: 仿探探左划右划
 *@creat time:14:21 2018/1/8
 **/

public class TanTanLayoutManager extends RecyclerView.LayoutManager  {
    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return null;
    }
//    private final ItemTouchHelper mTouchHelper;
//    private String Tag=TanTanLayoutManager.class.getSimpleName();
//    private RecyclerView mRecycleview;
//  
//
//    public TanTanLayoutManager(ItemTouchHelper touchHelper,RecyclerView mRecycleview) {
//       this.mTouchHelper=touchHelper;
//       this.mRecycleview=mRecycleview;
//    }
//
//   
//    /**
//     * 最大展示的条目个数
//     */
//    private static final int MAX_SHOW_NUM=3;
//
//  
//
//    @Override
//    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
//        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT);
//    }
//
//    @Override
//    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
//        removeAllViews();
//        //将所有布局放置到scrap缓冲池中
//        detachAndScrapAttachedViews(recycler);
//        int childCount = getItemCount();
//        if (childCount>MAX_SHOW_NUM){
//            for (int i = MAX_SHOW_NUM; i >=0; i--) {
//                View view = recycler.getViewForPosition(i);
//                addView(view);
//                //测量view
//                measureChildWithMargins(view,0,0);
//                //获取item的宽高间距
//                int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
//                int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
//                //摆放view(摆放到屏幕中间)
//                layoutDecorated(view,widthSpace/2,heightSpace/2,
//                        getDecoratedMeasuredWidth(view)+widthSpace/2,
//                        getDecoratedMeasuredHeight(view)+heightSpace/2
//                );
//                if (i==MAX_SHOW_NUM){
//                    view.setScaleX(1-i*0.1f);
//                    view.setScaleY(1-i*0.1f);
//                    view.setTranslationY(40*2);
//                }else if (i>0){
//                    view.setScaleX(1-i*0.1f);
//                    view.setScaleY(1-i*0.1f);
//                    view.setTranslationY(40f*i);
//                }else {
//                    view.setOnTouchListener(mTouchListener);
//                }
//                
//            }
//        }else {
//            for (int i = childCount-1; i >=0; i--) {
//                View view = recycler.getViewForPosition(i);
//                addView(view);
//                //测量view
//                measureChildWithMargins(view, 0, 0);
//                //获取item的宽高间距
//                int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
//                int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
//                //摆放view(摆放到屏幕中间)
//                layoutDecorated(view, widthSpace / 2, heightSpace / 2,
//                        getDecoratedMeasuredWidth(view) + widthSpace / 2,
//                        getDecoratedMeasuredHeight(view) + heightSpace / 2
//                );
//                if (i>0){
//                    view.setScaleX(1-i*0.1f);
//                    view.setScaleY(1-i*0.1f);
//                    view.setTranslationY(40f*i);
//                }else {
//                    view.setOnTouchListener(mTouchListener);
//                }
//            }
//        }
//
//
//    }
//
//    public View.OnTouchListener mTouchListener=new View.OnTouchListener() {
//        @Override
//        public boolean onTouch(View v, MotionEvent event) {
//            RecyclerView.ViewHolder childViewHolder = mRecycleview.getChildViewHolder(v);
//            if (event.getAction()==MotionEvent.ACTION_DOWN){
//                mTouchHelper.startSwipe(childViewHolder);
//            }
//            return false;
//        }
//    };
//    public static class CardItemTouchHelperCallBack extends ItemTouchHelper.Callback{
//        private String Tag=CardItemTouchHelperCallBack.class.getSimpleName();
//        public CardItemTouchHelperCallBack(List<String> datas, LayoutManagerActivity.MyAdapter adapter) {
//            this.datas = datas;
//            this.adapter=adapter;
//        }
//
//        private List<String> datas;
//        private LayoutManagerActivity.MyAdapter adapter;
//
//        @Override
//        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//            int dragFlag=0;
//            int swipeFlag=0;
//            if (recyclerView==null){
//                Log.e(Tag,"recyclerView==null");
//            }
//            if (recyclerView.getLayoutManager() instanceof TanTanLayoutManager){
//                swipeFlag=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT|ItemTouchHelper.UP|ItemTouchHelper.DOWN;
//            }
//            return makeMovementFlags(dragFlag,swipeFlag);
//        }
//
//        @Override
//        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//            return false;
//        }
//
//        @Override
//        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//            viewHolder.itemView.setOnTouchListener(null);
//            int layoutPosition = viewHolder.getLayoutPosition();
//            datas.remove(layoutPosition);
//            adapter.notifyDataSetChanged();
//            
//
//        }
//
//        @Override
//        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
//            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
//            View itemView = viewHolder.itemView;
//            if (actionState==ItemTouchHelper.ACTION_STATE_SWIPE){
//                float ratio=dX/recyclerView.getWidth()*getSwipeThreshold(viewHolder);
//                if (ratio>1){
//                    ratio=1;
//                }else if (ratio<-1) {
//                    ratio=-1;
//                }
//                itemView.setRotation(ratio * 15);
//                int childCount = recyclerView.getChildCount();
//                if (childCount>MAX_SHOW_NUM){
//                    for (int i = 1; i <childCount-1 ; i++) {
//                        int index = childCount - i - 1;
//                        View view = recyclerView.getChildAt(i);
//                        view.setScaleX(1-index*0.1f+ Math.abs(ratio)*0.1f);
//                        view.setScaleY(1-index*0.1f+ Math.abs(ratio)*0.1f);
//                        view.setTranslationY(40f*index+ Math.abs(ratio)*40f);
//                        
//                    }
//                }else {
//                    for (int i = 0; i <childCount-1 ; i++) {
//                        int index = childCount - i - 1;
//                        View view = recyclerView.getChildAt(i);
//                        view.setScaleX(1-index*0.1f+ Math.abs(ratio)*0.1f);
//                        view.setScaleY(1-index*0.1f+ Math.abs(ratio)*0.1f);
//                        view.setTranslationY(40f*index+ Math.abs(ratio)*40f);
//
//                    }
//                }
//            }
//        }
//    }
}
